﻿using System;
using Grundy.Common;
using Grundy.Message;

namespace Grundy.Entity
{
    public class FeatureIdentificationKeyMatchScoreEvaluator
    {
        public virtual int GetMatchScore(FeatureIdentificationKey a, FeatureIdentificationKey b)
        {
            var score = 0;

            if (a == FeatureIdentificationKey.Null || b == FeatureIdentificationKey.Null)
            {
                return 0;
            }

            if (a == b && a.LicenseId != Guid.Empty && a.FeatureId != Guid.Empty && a.ProductId != Guid.Empty && a.LicenseEntitlementId != Guid.Empty && a.CustomerId != Guid.Empty)
            {
                return int.MaxValue;
            }

            // Match feature id or name
            score += (GetScore(a.FeatureId, b.FeatureId) + GetScore(a.FeatureName, b.FeatureName));
           
            // Match license id or name
            score += (GetScore(a.LicenseId, b.LicenseId) + GetScore(a.LicenseName, b.LicenseName));
            
            // Match product id or name
            score += (GetScore(a.ProductId, b.ProductId) + GetScore(a.ProductName, b.ProductName));
            
            // Match license entitlement id
            score += GetScore(a.LicenseEntitlementId, b.LicenseEntitlementId);
            
            // Match customer id and name
            score += (GetScore(a.CustomerId, b.CustomerId) + GetScore(a.CustomerName, b.CustomerName));
            
            return score;
        }

        private int GetScore(Guid a, Guid b)
        {
            if (a == Guid.Empty || b == Guid.Empty)
                return 0;
            if (a != b)
                return -1000;
            return 1;
        }

        private int GetScore(string a, string b)
        {
            if (a.IsNullOrEmpty() || b.IsNullOrEmpty())
                return 0;
            if (!a.EqualsIgnoreCase(b))
                return -1000;
            return 1;
        }
    }
}